package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.Exame;
import com.model.TipoColmeia;

import com.model.Melgueiras;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table MELGUEIRAS.
*/
public class MelgueirasDao extends AbstractDao<Melgueiras, Long> {

    public static final String TABLENAME = "MELGUEIRAS";

    /**
     * Properties of entity Melgueiras.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_idMelgueira");
        public final static Property Designacao = new Property(1, String.class, "designacao", false, "DESIGNACAO");
        public final static Property DataColocacao = new Property(2, String.class, "dataColocacao", false, "DATA_COLOCACAO");
        public final static Property DataExtracacao = new Property(3, String.class, "dataExtracacao", false, "DATA_EXTRACACAO");
        public final static Property Tipo = new Property(4, String.class, "tipo", false, "TIPO");
        public final static Property Peso = new Property(5, Float.class, "peso", false, "PESO");
        public final static Property Tara = new Property(6, Float.class, "tara", false, "TARA");
        public final static Property Extraida = new Property(7, Boolean.class, "extraida", false, "EXTRAIDA");
        public final static Property Observacoes = new Property(8, String.class, "observacoes", false, "OBSERVACOES");
        public final static Property Isnote = new Property(9, Boolean.class, "isnote", false, "ISNOTE");
        public final static Property Updateinsert = new Property(10, Boolean.class, "updateinsert", false, "UPDATEINSERT");
        public final static Property CodApiario = new Property(11, long.class, "codApiario", false, "COD_APIARIO");
        public final static Property CodEnxame = new Property(12, long.class, "CodEnxame", false, "COD_ENXAME");
        public final static Property CodApicultor = new Property(13, long.class, "codApicultor", false, "COD_APICULTOR");
        public final static Property Tipoalca = new Property(14, long.class, "tipoalca", false, "TIPOALCA");
    };

    private DaoSession daoSession;

    private Query<Melgueiras> apiario_ApiarioMelgueirasQuery;
    private Query<Melgueiras> apicultor_ApicultorMelgueirasQuery;
    private Query<Melgueiras> exame_ExameMelgueirasQuery;
    private Query<Melgueiras> tipoColmeia_AlcaMelgueirasQuery;

    public MelgueirasDao(DaoConfig config) {
        super(config);
    }
    
    public MelgueirasDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'MELGUEIRAS' (" + //
                "'_idMelgueira' INTEGER PRIMARY KEY ASC AUTOINCREMENT ," + // 0: id
                "'DESIGNACAO' TEXT," + // 1: designacao
                "'DATA_COLOCACAO' TEXT," + // 2: dataColocacao
                "'DATA_EXTRACACAO' TEXT," + // 3: dataExtracacao
                "'TIPO' TEXT," + // 4: tipo
                "'PESO' REAL," + // 5: peso
                "'TARA' REAL," + // 6: tara
                "'EXTRAIDA' INTEGER," + // 7: extraida
                "'OBSERVACOES' TEXT," + // 8: observacoes
                "'ISNOTE' INTEGER," + // 9: isnote
                "'UPDATEINSERT' INTEGER," + // 10: updateinsert
                "'COD_APIARIO' INTEGER NOT NULL ," + // 11: codApiario
                "'COD_ENXAME' INTEGER NOT NULL ," + // 12: CodEnxame
                "'COD_APICULTOR' INTEGER NOT NULL ," + // 13: codApicultor
                "'TIPOALCA' INTEGER NOT NULL );"); // 14: tipoalca
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'MELGUEIRAS'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Melgueiras entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String designacao = entity.getDesignacao();
        if (designacao != null) {
            stmt.bindString(2, designacao);
        }
 
        String dataColocacao = entity.getDataColocacao();
        if (dataColocacao != null) {
            stmt.bindString(3, dataColocacao);
        }
 
        String dataExtracacao = entity.getDataExtracacao();
        if (dataExtracacao != null) {
            stmt.bindString(4, dataExtracacao);
        }
 
        String tipo = entity.getTipo();
        if (tipo != null) {
            stmt.bindString(5, tipo);
        }
 
        Float peso = entity.getPeso();
        if (peso != null) {
            stmt.bindDouble(6, peso);
        }
 
        Float tara = entity.getTara();
        if (tara != null) {
            stmt.bindDouble(7, tara);
        }
 
        Boolean extraida = entity.getExtraida();
        if (extraida != null) {
            stmt.bindLong(8, extraida ? 1l: 0l);
        }
 
        String observacoes = entity.getObservacoes();
        if (observacoes != null) {
            stmt.bindString(9, observacoes);
        }
 
        Boolean isnote = entity.getIsnote();
        if (isnote != null) {
            stmt.bindLong(10, isnote ? 1l: 0l);
        }
 
        Boolean updateinsert = entity.getUpdateinsert();
        if (updateinsert != null) {
            stmt.bindLong(11, updateinsert ? 1l: 0l);
        }
        stmt.bindLong(12, entity.getCodApiario());
        stmt.bindLong(13, entity.getCodEnxame());
        stmt.bindLong(14, entity.getCodApicultor());
        stmt.bindLong(15, entity.getTipoalca());
    }

    @Override
    protected void attachEntity(Melgueiras entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Melgueiras readEntity(Cursor cursor, int offset) {
        Melgueiras entity = new Melgueiras( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // designacao
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // dataColocacao
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // dataExtracacao
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // tipo
            cursor.isNull(offset + 5) ? null : cursor.getFloat(offset + 5), // peso
            cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6), // tara
            cursor.isNull(offset + 7) ? null : cursor.getShort(offset + 7) != 0, // extraida
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // observacoes
            cursor.isNull(offset + 9) ? null : cursor.getShort(offset + 9) != 0, // isnote
            cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0, // updateinsert
            cursor.getLong(offset + 11), // codApiario
            cursor.getLong(offset + 12), // CodEnxame
            cursor.getLong(offset + 13), // codApicultor
            cursor.getLong(offset + 14) // tipoalca
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Melgueiras entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setDesignacao(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setDataColocacao(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setDataExtracacao(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setTipo(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setPeso(cursor.isNull(offset + 5) ? null : cursor.getFloat(offset + 5));
        entity.setTara(cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6));
        entity.setExtraida(cursor.isNull(offset + 7) ? null : cursor.getShort(offset + 7) != 0);
        entity.setObservacoes(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setIsnote(cursor.isNull(offset + 9) ? null : cursor.getShort(offset + 9) != 0);
        entity.setUpdateinsert(cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0);
        entity.setCodApiario(cursor.getLong(offset + 11));
        entity.setCodEnxame(cursor.getLong(offset + 12));
        entity.setCodApicultor(cursor.getLong(offset + 13));
        entity.setTipoalca(cursor.getLong(offset + 14));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Melgueiras entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Melgueiras entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioMelgueiras" to-many relationship of Apiario. */
    public List<Melgueiras> _queryApiario_ApiarioMelgueiras(long codApiario) {
        synchronized (this) {
            if (apiario_ApiarioMelgueirasQuery == null) {
                QueryBuilder<Melgueiras> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApiario.eq(null));
                apiario_ApiarioMelgueirasQuery = queryBuilder.build();
            }
        }
        Query<Melgueiras> query = apiario_ApiarioMelgueirasQuery.forCurrentThread();
        query.setParameter(0, codApiario);
        return query.list();
    }

    /** Internal query to resolve the "apicultorMelgueiras" to-many relationship of Apicultor. */
    public List<Melgueiras> _queryApicultor_ApicultorMelgueiras(long codApicultor) {
        synchronized (this) {
            if (apicultor_ApicultorMelgueirasQuery == null) {
                QueryBuilder<Melgueiras> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodApicultor.eq(null));
                apicultor_ApicultorMelgueirasQuery = queryBuilder.build();
            }
        }
        Query<Melgueiras> query = apicultor_ApicultorMelgueirasQuery.forCurrentThread();
        query.setParameter(0, codApicultor);
        return query.list();
    }

    /** Internal query to resolve the "exameMelgueiras" to-many relationship of Exame. */
    public List<Melgueiras> _queryExame_ExameMelgueiras(long CodEnxame) {
        synchronized (this) {
            if (exame_ExameMelgueirasQuery == null) {
                QueryBuilder<Melgueiras> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodEnxame.eq(null));
                exame_ExameMelgueirasQuery = queryBuilder.build();
            }
        }
        Query<Melgueiras> query = exame_ExameMelgueirasQuery.forCurrentThread();
        query.setParameter(0, CodEnxame);
        return query.list();
    }

    /** Internal query to resolve the "alcaMelgueiras" to-many relationship of TipoColmeia. */
    public List<Melgueiras> _queryTipoColmeia_AlcaMelgueiras(long tipoalca) {
        synchronized (this) {
            if (tipoColmeia_AlcaMelgueirasQuery == null) {
                QueryBuilder<Melgueiras> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Tipoalca.eq(null));
                tipoColmeia_AlcaMelgueirasQuery = queryBuilder.build();
            }
        }
        Query<Melgueiras> query = tipoColmeia_AlcaMelgueirasQuery.forCurrentThread();
        query.setParameter(0, tipoalca);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getExameDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T3", daoSession.getTipoColmeiaDao().getAllColumns());
            builder.append(" FROM MELGUEIRAS T");
            builder.append(" LEFT JOIN APIARIO T0 ON T.'COD_APIARIO'=T0.'_idApiario'");
            builder.append(" LEFT JOIN APICULTOR T1 ON T.'COD_APICULTOR'=T1.'_idApicultor'");
            builder.append(" LEFT JOIN EXAME T2 ON T.'COD_ENXAME'=T2.'_idExame'");
            builder.append(" LEFT JOIN TIPO_COLMEIA T3 ON T.'TIPOALCA'=T3.'_idTipoColmeia'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Melgueiras loadCurrentDeep(Cursor cursor, boolean lock) {
        Melgueiras entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }
        offset += daoSession.getExameDao().getAllColumns().length;

        TipoColmeia tipoColmeia = loadCurrentOther(daoSession.getTipoColmeiaDao(), cursor, offset);
         if(tipoColmeia != null) {
            entity.setTipoColmeia(tipoColmeia);
        }

        return entity;    
    }

    public Melgueiras loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Melgueiras> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Melgueiras> list = new ArrayList<Melgueiras>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Melgueiras> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Melgueiras> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
